import gtk,gobject
import WindowTemplate

class PreferencesWindow(WindowTemplate.WindowTemplate):
    """The preferences window"""
    
    def __init__(self,controller):
        """constuctor"""
        WindowTemplate.WindowTemplate.__init__(self,controller)
        self.create_window()
        self.fill()
        self.cb_hide.connect("clicked",self.edit_preference_callback,self.config.user,'START HIDE')
        self.cb_startup.connect("clicked",self.edit_preference_callback,self.config.user,'STARTUP START')
        self.cb_unmount_on_exit.connect("clicked",self.edit_preference_callback,self.config.user,'UMOUNT ON EXIT')  
    
    def create_window(self):
        self.set_title('Preferences')
        self.global_vbox = gtk.VBox(False,5)
        self.add(self.global_vbox)
        
        #Start Hide
        lbl_hide = gtk.Label(' Start hide in tray ')
        self.cb_hide = gtk.CheckButton()
        self.global_vbox.pack_start(self.build_hbox((lbl_hide,self.cb_hide)))
        
        #Startup Start
        lbl_startup = gtk.Label('Start on startup')
        self.cb_startup = gtk.CheckButton()
        self.global_vbox.pack_start(self.build_hbox((lbl_startup,self.cb_startup)))
        
        #Umount Shares on Exit
        lbl_umount_on_exit = gtk.Label('Umount shares on exit')
        self.cb_unmount_on_exit = gtk.CheckButton()
        self.global_vbox.pack_start(self.build_hbox((lbl_umount_on_exit,self.cb_unmount_on_exit)))
        
        #Default Mount Point
        lbl_mount_point = gtk.Label('Mount Point')
        self.tb_mount_point = gtk.Entry()
        self.add_validator(self.tb_mount_point,'REGEX_MOUNT_POINT',False)
        
        btn_mount_point = gtk.Button(None,gtk.STOCK_OPEN)
        btn_mount_point.connect('clicked',self.btn_mount_point_callback)
        self.global_vbox.pack_start(self.build_hbox((lbl_mount_point,self.tb_mount_point,btn_mount_point)))
        
        btn_ok = gtk.Button(None,gtk.STOCK_APPLY)
        btn_ok.connect('clicked',self.submit)
        self.global_vbox.pack_start(self.build_hbox((btn_ok,)))
        
        self.show_all()
        
    def fill(self):
        self.cb_hide.set_active(self.config.user['START HIDE'])
        self.cb_startup.set_active(self.config.user['STARTUP START']) 
        self.cb_unmount_on_exit.set_active(self.config.user['UMOUNT ON EXIT'])
        self.tb_mount_point.set_text(self.config.user['DEFAULT MOUNT POINT'])
    
        
    def btn_mount_point_callback(self,btn):
        fcd = gtk.FileChooserDialog('Default Mount Point', self, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,(gtk.STOCK_OK, gtk.RESPONSE_OK))
        if fcd.run() == gtk.RESPONSE_OK:
            self.config.user['DEFAULT MOUNT POINT'] = fcd.get_filename()
            self.tb_mount_point.set_text(fcd.get_filename())
        fcd.destroy()
    
    def edit_preference_callback(self,cb,dict,key):
        dict[key] = cb.get_active()
    
    def submit(self,*args):
        if self.is_valid():
            tb_text = self.tb_mount_point.get_text()
            if tb_text[len(tb_text)-1] != '/':
                tb_text += '/'
            self.config.user['DEFAULT MOUNT POINT'] = tb_text
        self.close()